All files / web/src/app/api/enrollment-requests/[requestId]/deny route.ts

0% Statements 0/76
0% Branches 0/1
0% Functions 0/1
0% Lines 0/76

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77                                                                                                                                                         
import { eq } from 'drizzle-orm'
import { NextResponse } from 'next/server'
import { withAuth } from '@/lib/auth/withAuth'
import { db, schema } from '@/db'
import { enrollmentRequests } from '@/db/schema'
import { denyEnrollmentRequest, isParentOf } from '@/lib/classroom'
import { emitEnrollmentRequestDenied } from '@/lib/classroom/socket-emitter'
import { getUserId } from '@/lib/viewer'

/**
 * POST /api/enrollment-requests/[requestId]/deny
 * Parent denies enrollment request
 *
 * Returns: { request }
 */
export const POST = withAuth(async (_request, { params }) => {
  try {
    const { requestId } = (await params) as { requestId: string }
    const userId = await getUserId()

    // Get the request to verify parent owns the child
    const request = await db.query.enrollmentRequests.findFirst({
      where: eq(enrollmentRequests.id, requestId),
    })

    if (!request) {
      return NextResponse.json({ error: 'Request not found' }, { status: 404 })
    }

    // Verify user is a parent of the child in the request
    const parentCheck = await isParentOf(userId, request.playerId)
    if (!parentCheck) {
      return NextResponse.json({ error: 'Not authorized' }, { status: 403 })
    }

    const updatedRequest = await denyEnrollmentRequest(requestId, userId, 'parent')

    // Emit socket event for real-time updates (notify teacher via classroom channel)
    try {
      // Get classroom and player info for socket event
      const [classroomInfo] = await db
        .select({ name: schema.classrooms.name })
        .from(schema.classrooms)
        .where(eq(schema.classrooms.id, request.classroomId))
        .limit(1)

      const [playerInfo] = await db
        .select({ name: schema.players.name })
        .from(schema.players)
        .where(eq(schema.players.id, request.playerId))
        .limit(1)

      if (classroomInfo && playerInfo) {
        await emitEnrollmentRequestDenied(
          {
            requestId,
            classroomId: request.classroomId,
            classroomName: classroomInfo.name,
            playerId: request.playerId,
            playerName: playerInfo.name,
            deniedBy: 'parent',
          },
          { classroomId: request.classroomId } // Teacher sees the update via classroom channel
        )
      }
    } catch (socketError) {
      console.error('[Parent Deny] Failed to emit socket event:', socketError)
    }

    return NextResponse.json({ request: updatedRequest })
  } catch (error) {
    console.error('Failed to deny enrollment request:', error)
    const message = error instanceof Error ? error.message : 'Failed to deny enrollment request'
    return NextResponse.json({ error: message }, { status: 500 })
  }
})